#ifndef MAINAREA_H
#define MAINAREA_H

#include <QtGui/QScrollArea>
#include <QMouseEvent>
#include <QCursor>
#include <vector>
#include <QPrintPreviewDialog>
#include <QPainter>
#include <QPrinter>
#include <QColor>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QIODevice>
#include <QByteArray>
#include <QHBoxLayout>
#include <QByteArray>
#include <QFontDialog>
#include <QColorDialog>
#include <QFrame>

#include <time.h>
#include <typeinfo>

#include "resize_tool.h"
#include "smart_byte_array.h"
#include "area.h"
#include "text_area.h"
#include "image_area.h"
#include "file_area.h"
#include "pin_area.h"
#include "vector_line.h"

using namespace std;

enum AbsractNextAction {CURSOR = 0, TEXT = 1, IMG = 2, LINK = 3, PIN = 4};

class MainArea : public QFrame
{
    Q_OBJECT
    // all elements stored here
    vector<Area*> element;
    vector<VectorLine*> line;

    bool mouseDown;
    QPoint mouseGotDown, mouseNow;
    void check_for_bad_lines();
    QByteArray previous_state;
    ResizeTool *drag_frame;
public:
    bool parse_data(SmartByteArray data);
    void zoom_in();
    void zoom_out();

    void load_next_document();

    void set_font_for_areas();
    void set_color_for_areas();
    void restore_from_ex_buffer(QByteArray buffer);
    void areas_to_grid();
    void printPreview();
    void deleteSelectedAreas();
    void deleteAreas();
    AbsractNextAction nextAction;
    MainArea(QWidget* parent=0);
    ~MainArea();
    void mousePressEvent ( QMouseEvent * e );
    void mouseReleaseEvent ( QMouseEvent * e );
    void mouseMoveEvent ( QMouseEvent * e );
    void paintEvent(QPaintEvent *e);
    void resizeEvent(QResizeEvent *);
    void selectAll();

    void refresh() { this->update(); }

    // Storing functions
    QByteArray Store(); // Genetate text info about elements
    QByteArray store_selected(); // Genetate text info about elements
    void store_to_buffer();
    QByteArray restore_from_buffer();

    // Elements functions
    void addText(int X=0, int Y=0, int W=250, int H=80, int serial=-1, const QString & data = "");
    void addImage(int X=0, int Y=0, int W=250, int H=80, int serial=-1, const QByteArray & data = "", bool default_size = true);
    void addFile(int X=0, int Y=0, int W=250, int H=80, int serial=-1, const QString & data = "");
    void addPin(int X=0, int Y=0, int W=250, int H=80, int serial=-1, const QString & data = "");
    void addLine();
    void delLine();
    void addLine(Area *src, Area *dest);

    Area *get_area_by_serial(int serial);
    bool ask4confirm();

    bool grid_mode;

private:

    enum AreaType { TextType, ImageType, FileType, PinType };
    Area * addArea(int x, int y, int width, int height, int serial, AreaType type);

private slots:

    void printDocument( QPrinter *printer_ );
    void deleteArea(int id);
    void moveSelectedAreas(int x, int y);
    void unselectAll(int eid);
    void resize(int deltaWidth, int deltaHeight);

};

#endif // MAINAREA_H
